package com.begye.smartloan.apptest.shenhezhong

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.begye.smartloan.apptest.MyApplication
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtShenHeZhongBinding
import com.begye.smartloan.apptest.detailadsource.DetailAdSourceViewModel
import com.begye.smartloan.apptest.ext.addColor
import com.begye.smartloan.apptest.fangkuanzhong.FangKuanZhongActivity
import com.begye.smartloan.apptest.gongong.SysConfigViewModel
import com.begye.smartloan.apptest.gongong.YongHuLiuChengViewModel
import com.begye.smartloan.apptest.homeadsource.HomeAdSourceActivity
import com.begye.smartloan.apptest.huankuan.HuanKuanActivity
import com.begye.smartloan.apptest.huankuan.HuanKuanShenHeZhongActivity
import com.begye.smartloan.apptest.jujue.JuJueActivity
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.pingjia.PingJiaDialogFragment
import com.begye.smartloan.apptest.renzheng.RenZhengActivity
import com.begye.smartloan.apptest.renzheng.bangka.RenZhengBangKaActivity
import com.begye.smartloan.apptest.renzheng.document.RenZhengDocumentActivity
import com.begye.smartloan.apptest.renzheng.lianxiren.RenZhengLianXiRenActivity
import com.begye.smartloan.apptest.renzheng.selfie.RenZhengSelfieActivity
import com.begye.smartloan.apptest.shenhezhong.adapter.ShenHeZhongQianBaoAdapter
import com.begye.smartloan.apptest.shenhezhong.dialog.NotEnoughQianBaoDialogFragment
import com.begye.smartloan.apptest.shenhezhong.dialog.YingYongTongZhiDialogFragment
import com.begye.smartloan.apptest.tool.PageTagTool
import com.begye.smartloan.apptest.tool.SysConfigTool
import com.begye.smartloan.apptest.tool.addTouch
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity


@SuppressLint("CustomWelcomeScreen")
class ShenHeZhongActivity : AppCompatActivity() {

    private lateinit var _binding: AtShenHeZhongBinding
    private lateinit var _shenHeZhongQianBaoAdapter: ShenHeZhongQianBaoAdapter

    private val _sysConfigViewModel by lazy {
        ViewModelProvider(this)[SysConfigViewModel::class.java]
    }

    private val _shenHeZhongViewModel by lazy {
        ViewModelProvider(this)[ShenHeZhongViewModel::class.java]
    }

    private val _detailAdSourceViewModel by lazy {
        ViewModelProvider(this)[DetailAdSourceViewModel::class.java]
    }

    private val _yongHuLiuChengViewModel by lazy {
        ViewModelProvider(this)[YongHuLiuChengViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtShenHeZhongBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configYongHuLiuChengState() {

        _shenHeZhongViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _detailAdSourceViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onHasOrderStatusLiveData.observe(this) {
            it?.let { result ->
                when (result) {
                    PageTagTool.dai_huan_kuan -> {
                        toNextActivity(
                            activity = HuanKuanActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.fang_kuan_zhong -> {
                        toNextActivity(
                            activity = FangKuanZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.shen_he_zhone -> {
//                        toNextActivity(
//                            activity = ShenHeZhongActivity::class.java,
//                            mode = IntentFlagMode.SINGLE_TOP
//                        )
                        if (_shenHeZhongViewModel.canShowPingJiaKuan()) {
                            _shenHeZhongViewModel.requestPingJiaConfig()
                        }

                        _sysConfigViewModel.requestSystemConfiguration()
                    }

                    PageTagTool.ju_jue -> {
                        toNextActivity(
                            activity = JuJueActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                }
            }
        }

        _yongHuLiuChengViewModel.onDoingRenZhengPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingLianXiRenPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengLianXiRenActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingBangKaFrPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengBangKaActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingDocumentPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengDocumentActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingSelfiePageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengSelfieActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onAllDoneLiveData.observe(this) {
            if (it.isNotBlank()) {
                // to shen qing ye
                toNextActivity(
                    activity = HomeAdSourceActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun configUIState() {

        _detailAdSourceViewModel.onPreviewDetailLiveData.observe(this) {
            it?.let { result ->
                try {
                    _binding.atShenHeZhongJinE.text = result.optString("amountFormat", "")
                    _binding.atShenHeZhongTianShuValue.text = result.optString("days", "")
                    _binding.atShenHeZhongCurrency.text = result.optString("currency", "")
                } catch (e: Exception) {
                    //
                }
                _shenHeZhongViewModel.requestQianBaoList()
            }
        }

        _shenHeZhongViewModel.onQianBaoListLiveData.observe(this) {
            it?.let { result ->
                _shenHeZhongQianBaoAdapter.dataList = result
                _shenHeZhongQianBaoAdapter.notifyDataSetChanged()

                if (MyApplication.instance.isTargetServerFr()) {
                    if (result.size < 2) {
                        val dialog = NotEnoughQianBaoDialogFragment {
                            toNextActivity(
                                activity = RenZhengBangKaActivity::class.java,
                                mode = IntentFlagMode.NEW_TASK
                            )
                        }
                        dialog.showNow(supportFragmentManager, "notEnoughQianBao")
                    }
                }

                _yongHuLiuChengViewModel.requestYongHuLiuCheng()
            }
        }

        _shenHeZhongViewModel.onNeedPingJiaStateLiveData.observe(this) {
            if (it == true) {
                try {
                    _shenHeZhongViewModel.didShowPingJiaKuan()

                    val dialog = PingJiaDialogFragment(this@ShenHeZhongActivity)
                    dialog.showNow(supportFragmentManager, System.currentTimeMillis().toString())

                } catch (e: Exception) {
                    //
                }
            }
        }

        _sysConfigViewModel.onSysConfigSuccessfulLiveData.observe(this) {
            try {
                val text = SysConfigTool.filterYingYongTongZhi()
                if (text.isNotBlank()) {
                    val dialog = YingYongTongZhiDialogFragment(text)
                    dialog.showNow(supportFragmentManager, System.currentTimeMillis().toString())
                }
            } catch (e: Exception) {
                //
            }
        }
    }

    private fun configUIEvent() {

        _binding.atShenHeZhongNarBar.initConfig(
            leftResId = R.drawable.dres_139,
            rightResId = R.drawable.dres_84
        ) {
            if (0 == it) {
                _binding.atShenHeZhongCouTiDrawer.refresh(this@ShenHeZhongActivity) {
                    if (_binding.atShenHeZhongRootLayout.isDrawerOpen(_binding.atShenHeZhongCouTiDrawer)) {
                        _binding.atShenHeZhongRootLayout.closeDrawer(_binding.atShenHeZhongCouTiDrawer)
                    }
                }

                if (_binding.atShenHeZhongRootLayout.isDrawerOpen(_binding.atShenHeZhongCouTiDrawer)) {
                    _binding.atShenHeZhongRootLayout.closeDrawer(_binding.atShenHeZhongCouTiDrawer)
                } else {
                    _binding.atShenHeZhongRootLayout.openDrawer(_binding.atShenHeZhongCouTiDrawer)
                }
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _shenHeZhongQianBaoAdapter = ShenHeZhongQianBaoAdapter(emptyList())
        _binding.atShenHeZhongQianBaoList.layoutManager = LinearLayoutManager(this)
        _binding.atShenHeZhongQianBaoList.adapter = _shenHeZhongQianBaoAdapter

        _binding.atShenHeZhongBottomBarHuanKuan.setOnClickListener {
            toNextActivity(
                activity = HuanKuanShenHeZhongActivity::class.java,
                mode = IntentFlagMode.SINGLE_TOP
            )
        }

        _binding.atShenHeZhongRefresh.setOnClickListener {
            configUIData()

            //
            addTouch(type = "shen_he_zhong_refresh_click")
        }

        _binding.atShenHeZhongTips.addColor(
            words = getString(R.string.shen_he_zhong_text_3),
            hexColor = "#000000"
        )

        if (MyApplication.instance.isTargetServerFr()) {
            _binding.atShenHeZhongQianBaoAddOrEdit.visibility = View.VISIBLE
        } else {
            _binding.atShenHeZhongQianBaoAddOrEdit.visibility = View.GONE
        }
    }

    private fun configUIData() {
        _detailAdSourceViewModel.requestPreviewDetail()
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

